package com.fin.zw.aiqas.controller;


import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.model.request.LoginRequest;
import com.fin.zw.aiqas.model.request.PasswordRequest;
import com.fin.zw.aiqas.model.request.UserIds;
import com.fin.zw.aiqas.model.request.UserRoleRequest;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/loginController")
@Api(value = "账号管理",description = "账号管理")
public class LoginController {

    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @GetMapping("/patchca")
    @ApiOperation(value = "获取验证码",notes = "获取验证码")
    public void patchca(HttpServletRequest request,
                        HttpServletResponse response) throws Exception {
        loginService.patchca(request, response);
    }

    @PostMapping("/unlockUserInfo")
    @ApiOperation(value = "对账号进行解锁",notes = "对账号进行解锁")
    public ResponseEntity<String> unlockUserInfo(@RequestBody UserIds userIds){
        loginService.unlockUserInfo(userIds);
        return ResponseEntity.ok().body("解锁成功");
    }

    @PostMapping("/updatePassword")
    @ApiOperation(value = "修改账号密码", notes = "修改账号密码")
    public ResponseEntity<Void> updatePassword(@RequestBody PasswordRequest request,
                                               @ApiIgnore @CurrentUser SessionModel sessionModel) {
        loginService.updatePassword(sessionModel, request);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/resetPassword")
    @ApiOperation(value = "密码重置",notes = "密码重置")
    public ResponseEntity<String> resetPassword(@RequestBody UserIds userIds){
        loginService.resetPassword(userIds);
        return ResponseEntity.ok().body("密码重置成功");
    }

    @PostMapping("/assignmentRole")
    @ApiOperation(value = "对账号进行赋角色",notes = "对账号进行赋角色")
    public ResponseEntity<String> assignmentRole(@RequestBody UserRoleRequest request){
        loginService.assignmentRole(request);
        return ResponseEntity.ok().body("赋角色成功");
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登陆",notes = "用户登录")
    public ResponseEntity<SessionModel> login(@RequestBody LoginRequest loginRequest,
                                              HttpServletRequest request) throws Exception {
        SessionModel sessionModel = loginService.login(loginRequest, request);
        return ResponseEntity.ok().body(sessionModel);
    }

    @GetMapping("/getToken")
    @ApiOperation(value = "获取token",notes = "获取token")
    public ResponseEntity<String> getToken(@RequestParam("loginCode") String loginCode,
                                           @RequestParam("password") String password) throws Exception {
        String token = loginService.getToken(loginCode, password);
        return ResponseEntity.ok().body(token);
    }


}
